from tkinter import *
import tkinter as tk
import tkinter.messagebox as messagebox
import requests
import ctypes
import sys
import json
import wmi
import threading
from LocalClient import LocalClient
from utils.SystemInfo import SystemInfo


class GUI():
    def __init__(self, root):
        self.__username = ""
        self.__is_register = False
        self.__uuid = SystemInfo.get_uuid()
        self.ininGUI(root)
     

    def ininGUI(self, window):
        # 窗口设置
        winWidth = 320
        winHeight = 180

        screenWidth = window.winfo_screenwidth()
        screenHeight = window.winfo_screenheight()

        x = int((screenWidth - winWidth) / 2)
        y = int((screenHeight - winHeight) / 2)

        window.title("基线安全检查")

        window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))

        window.iconbitmap("./resources/icon.ico")

        window.resizable(0, 0)

        username_var = tk.StringVar()
        pwd_var = tk.StringVar()

        tk.Label(window, text="用户名").grid(row=0)
        tk.Label(window, text="密码").grid(row=1)

        self.__e1 = Entry(window, textvariable=username_var)
        self.__e1.grid(row=0, column=2)

        self.__e2 = Entry(window, show="*", textvariable=pwd_var)
        self.__e2.grid(row=1, column=2)

        tk.Button(text="登录",  command=self.__login, width=10).grid(
            row=0, rowspan=2, column=3, columnspan=3, padx=5, pady=5)

        self.__login_status = tk.StringVar()
        self.__login_status.set("未登录")
        tk.Label(window, textvariable=self.__login_status).grid(
            row=0, column=8, rowspan=2, pady=5)

        tk.Button(text="注册机器",  command=self.__register, width=20).grid(
            row=3, rowspan=2, column=2, columnspan=4, pady=20)

        tk.Button(text="开始扫描",  command=self.__check, width=20).grid(
            row=5, rowspan=2, column=2, columnspan=4)

        menu_bar = Menu(window)
        window.config(menu=menu_bar)

        setting_menu = Menu(menu_bar, tearoff=0)
        setting_menu.add_command(label="关于", command=self.__about)
        setting_menu.add_separator()
        setting_menu.add_command(label="退出", command=self.__quit)
        menu_bar.add_cascade(label="设置", menu=setting_menu)

        window.mainloop()

    # 登录
    def __login(self):
        if self.__username !=  "":
            messagebox.showerror("错误", "请勿重复登录")

        url = 'http://127.0.0.1:8080/shiro/login'
        postdatas = {
            "username": self.__e1.get(),
            "password": self.__e2.get()
        }
        req = requests.post(url, data=postdatas)
        if req.status_code == 200:
            self.__username = req.json()["username"]
            self.__login_status.set("已登录")
            messagebox.showinfo("登录成功", "访问网站获得详情")
        else:
            messagebox.showerror("登录失败", "请重试")

    # 注册机器
    def __register(self):
        if self.__login_status.get() == "未登录":
            messagebox.showerror("错误", "请先登录")
            return
        
        caption = wmi.WMI().Win32_OperatingSystem()[0].Caption
        CSName = wmi.WMI().Win32_OperatingSystem()[0].CSName
        OSArchitecture = wmi.WMI().Win32_OperatingSystem()[0].OSArchitecture

        url = 'http://127.0.0.1:8080/device/insert'
        postdatas = {
            "username":  self.__username,
            "uuid": self.__uuid,
            "caption": caption,
            "csname": CSName,
            "osarchitecture": OSArchitecture
        }
        headers = {"Content-Type": "application/json"}
        data = json.dumps(postdatas)
        req = requests.post(url, headers=headers, data=data)

        if req.status_code == 200:
            self.__is_register = True
            messagebox.showinfo("注册机器成功", "访问网站获得详情")
        else:
            messagebox.showerror("注册失败", "请勿重复注册")

    # 基线检查
    def __check(self):
        if self.__login_status.get() == "未登录":
            messagebox.showerror("错误", "请先登录")
            return

        if self.__is_register == False:
            url = 'http://127.0.0.1:8080/device/selectById'
            postdatas = {
                "uuid": self.__uuid,
            }
            req = requests.post(url, data=postdatas)
            if req.status_code == 201:
                messagebox.showerror("错误", "请先注册机器")
                return
            else:
                is_register = True

        T = threading.Thread(target=self.__client_socket)
        T.start()

    def __client_socket(self):
        myLocalClient = LocalClient("127.0.0.1", 9999, self.__username)
        messagebox.showinfo("上传成功", "访问网站获得详情")
        myLocalClient.scan_all_info()
        

    # 菜单栏
    def __about(self):
        messagebox.showinfo("关于", "Author:Yawn \nDate:2021.6.16")

    def __quit(self,window):
        window.quit()
        window.destroy()
        exit()


if __name__ == "__main__":
    root = Tk()
    myGUI = GUI(root)
